/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.in;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.biz.crawler.UrlValidator;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;

/**
 * ClassName:TimesOfIndiaNewsCrawler <br/>
 * Date: 2018年11月26日 下午7:41:00 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class TimesOfIndiaNewsCrawler extends NewsCrawler {

    @Resource
    private TimesOfIndiaNewsCrawler             timesOfIndiaNewsCrawler;
    @Resource
    private TimesOfIndiaNewsContentImageHandler timesOfIndiaNewsContentImageHandler;

    @Override
    public List<SourceType> getSourceTypeList() {
        List<SourceType> sourceTypeList = new ArrayList<SourceType>();
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/blogs/").newsType(NewsType.BLOG).urlValidator(new UrlValidator() {

            @Override
            public boolean isOkUrl(String url) {
                boolean result = timesOfIndiaNewsCrawler.isOkUrl(url);
                if (!result) {
                    return result;
                }
                if (url.indexOf("/author/") > 0 || url.indexOf("/tag/") > 0 || url.indexOf("/city/") > 0) {
                    return false;
                }
                try {
                    URI uri = new URI(url);
                    String path = uri.getPath();
                    String[] pathArr = path.split("/");
                    if (pathArr[pathArr.length - 1].length() < 30) {
                        return false;
                    }
                } catch (URISyntaxException e) {
                    return false;
                }

                return result;
            }
        }).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/business").newsType(NewsType.BUSINESS).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/city").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/education").newsType(NewsType.EDUCATION).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/elections").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/tv/hindi").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/etimes").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("https://photogallery.indiatimes.com/").newsType(NewsType.IMAGE).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/india").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/life-style").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/sports").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.cricbuzz.com").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.gadgetsnow.com").newsType(NewsType.TECH).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/live-tv").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/videos").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("https://timesofindia.indiatimes.com/world").newsType(NewsType.NEWS).build());
        return sourceTypeList;
    }

    /**
     * 该url是否可用的新闻链接
     * 
     * @param url
     * @return
     */
    public boolean isOkUrl(String url) {
        URI uri;
        try {
            if (url == null || url.trim().length() == 0) {
                return false;
            }
            if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
                return false;
            }
            /*
             * if (!(url.endsWith(".cms") || url.indexOf(".cms?") > 0)) { return false; }
             */
            uri = new URI(url);
            String host = uri.getHost();
            String path = uri.getPath();
            if(host==null) {
                return false;
            }
            if ("www.gadgetsnow.com".equals(host) || path.indexOf("/india/") >= 0) {// blog的文章拆分出来是5级
                if (path.split("/").length < 5) {
                    return false;
                }
            } else if ("www.cricbuzz.com".equals(host) || path.indexOf("/blogs/") >= 0) {
                if (path.split("/").length < 4) {
                    return false;
                }
            } else if (path.split("/").length < 6) {
                return false;
            }
            return true;
        } catch (URISyntaxException e) {
        }
        return false;
    }

    @Override
    public ContentImageHandler getContentImageHandler() {
        return timesOfIndiaNewsContentImageHandler;
    }

    @Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.IN.name());
        return countryCodeSet;
    }
}
